package demo;

import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 *   合并K个排序链表
 */
class Solution27 {
	public ListNode mergeKLists(ListNode[] lists) {
		PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
			@Override
			public int compare(ListNode o1, ListNode o2) {
				return o1.val - o2.val;
			}
		});
		for (ListNode listNode : lists) {
			if (listNode != null) {
				queue.add(listNode);
			}
		}
		ListNode dummy = new ListNode(0);
		ListNode curr = dummy;
		while (!queue.isEmpty()) {
			ListNode node = queue.poll();
			curr.next = node;
			curr = curr.next;
			queue.offer(curr);
		}
		return dummy.next;
	}
}

